/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dbFish;

import java.sql.SQLException;

/**
 * Exception to be thrown by all HashPersistence methods. <p>
 *
 * This class encapsulates all problems that may happen in the
 * HashPersistence class methods. <p>
 *
 * One interesting feature is this: the SQL query which caused the exception is
 * recorded here, and may be requested later for easier debugging. <p>
 */
public class HashPersistenceException extends Exception {

	/**
	 * Error message to be given to the user. <p>
	 */
	private String message;

	/**
	 * The SQL query that caused this exception. <p>
	 */
	private String query;


	/**
	 * SQLException returned by database
	 */
	private SQLException sqlException;

	/**
	 * Default Constructor. <p>
	 *
	 * When this constructor is used, the short message is initialized to
	 * "Unknown error" -- so avoid it as much as you can. <p>
	 *
	 */
	public HashPersistenceException () {
		message="Unknow Error";
	}

	public HashPersistenceException(Throwable cause) {
		super(cause);
	}

	/**
	 * Constructor that gets the short message. <p>
	 *
	 * @param message A String with the short message for the exception.
	 */
	public HashPersistenceException (String message) {
		this.message = message;
	}

	/**
	 * Constructor that gets the short message and SQL query. <p>
         *
         * This constructor initializes the short error message and also
         * the String with the SQL query which caused the exception. <p>
	 *
	 * This is useful for debugging (it helps understand why a method
	 * failed). <p>
	 *
	 * @param message The short message for the exception.
	 * @param query The SQL query which caused the exception.
	 */
	public HashPersistenceException (String message, String query) {
		this.message = message;
		this.query = query;
	}



	/**
	 * Constructor that gets the short message, SQL Query and Database
	 * error messag.
	 * This constructor initializes the short error message and also
	 * the String with the SQL query which caused the exception. <p>
	 *
	 * @param message The short message of Exception
	 * @param query  The SQL query which caused the exception
	 * @param exc SQL Exception
	 */
	public HashPersistenceException (String message, String query,
			SQLException exc) {
		this.message = message;
		this.query = query;
		this.sqlException = exc;
	}

	public HashPersistenceException(String message, Throwable cause) {
		super(message, cause);
	}

	/**
	 * Returns the short error message. <p>
         *
         * This method returns the short error message for this exception. <p>
         *
         * @return A String with the short error message.
	 */
	public String getMessage() {
		if (sqlException != null)
			return message + ":" + sqlException;
		return message;
	}

	/**
	 * Returns the query which caused the error. <p>
         *
         * When something wrong happens, this method can be used to get a String
         * with the SQL query that caused the Exception to be thrown. <p>
         *
         * @return A String with the SQL query that caused this exception.
	 */
	public String getQuery() {
		return query;
	}

	/**
	 * Returns the SQLException returned by database. <p>
	 *
	 * If this HashPersistenceException was caused by an SQLException, this method
	 * can be used to get it, so debugging will be easier. <p>
	 *
	 * @return The SQLException generated by the JDBC driver.
	 */
	public SQLException getSQLException() {
		return sqlException;
	}


}

